package implicits

import com.github.tototoshi.csv.{CSVReader, CSVWriter}
import org.apache.commons.lang3.StringUtils
import org.apache.poi.xssf.usermodel.XSSFWorkbook

import scala.collection.immutable.SeqMap
import implicits.Implicits._

import java.io.{File, FileOutputStream}

/**
 * Created by Administrator on 2019/9/12
 */
trait CSVTool {

  implicit class CSVFile(file: File) {

    def csvLines = {
      val reader = CSVReader.open(file)
      val lines = reader.all()
      reader.close()
      lines
    }
  }

  implicit class CSVLines(lines: List[List[String]]) {

    def toFile(file: File) = {
      val writer = CSVWriter.open(file)
      writer.writeAll(lines)
      writer.close()
    }

    def convertHeader(map: Map[String, String]) = {
      val newHeaders = lines.head.map { header =>
        map.getOrElseIgnoreCase(header, header)
      }
      newHeaders +: lines.drop(1)
    }

    def lineMap = {
      val headers = lines.head.toLowerCase
      lines.drop(1).map { columns =>
        headers.map(_.toLowerCase).zip(columns).toMap
      }
    }

    def lineMapNoLower = {
      val headers = lines.head.toLowerCase
      lines.drop(1).map { tmpColumns =>
        val t = headers.map(x => x).zip(tmpColumns)
        val x = SeqMap.from(t)
        x
      }
    }

    def lineSeqMapLower = {
      val headers = lines.head.toLowerCase
      lines.drop(1).map { tmpColumns =>
        val columns = tmpColumns.padTo(headers.size, "")
        headers.zip(columns).toSeqMap
      }
    }

    def toTxtFile(file: File) = {
      lines.map(_.mkString("\t")).toFile(file)
    }

    def lineSeqMap = {
      val headers = lines.head
      lines.drop(1).map { tmpColumns =>
        val columns = tmpColumns.padTo(headers.size, "")
        headers.zip(columns).toSeqMap
      }
    }

    def lineMapNoLower(data: Map[String, String]) = {
      //带有别名的 一行
      val map = data
      var headers = lines.head.map(x => x.toLowerCase)
      println("map = " + map)
      println("headers = " + headers)
      headers = headers.map {
        x => {
          if (map.contains(x)) {
            map(x).toLowerCase()
          }
          else x.toLowerCase()
        }
      }
      val res = lines.drop(1).map { tmpColumns =>
        val t = headers.map(x => x).zip(tmpColumns)
        val x = SeqMap.from(t)
        x
      }
      res
    }


    def selectOneColumn(columnName: String) = {
      val maps = lineMap
      maps.map { map =>
        map(columnName.toLowerCase)
      }
    }

    def mapOtherByColumns[T](f: List[String] => T) = {
      lines.map { columns =>
        f(columns)
      }

    }

    def notEmptyLines = lines.filter(x => x.exists(y => StringUtils.isNotEmpty(y)))

    def toXlsxFile(xlsxFile: File) = {
      val outputWorkbook = new XSSFWorkbook()
      val outputSheet = outputWorkbook.createSheet("Sheet1")
      for (i <- 0 until lines.size) {
        val outputEachRow = outputSheet.createRow(i)
        val columns = lines(i)
        for (j <- 0 until columns.size) {
          val cell = outputEachRow.createCell(j)
          if (columns(j).isDouble && i != 0 && j!=0) {
            cell.setCellValue(columns(j).toDouble)
          } else {
            cell.setCellValue(columns(j))
          }

        }
      }

      val fileOutputStream = new FileOutputStream(xlsxFile)
      outputWorkbook.write(fileOutputStream)
      fileOutputStream.close()
      outputWorkbook.close()
    }

  }


}
